/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"1321EAF1-1F05-444D-9B52-652B05DF1E88",variableType:8}
 */
var fTotalComprob = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"34A555FE-B744-4895-835F-C6DA74E3EB5A",variableType:4}
 */
var fSelecciono = 0;

/** 
 *
 * @properties={typeid:24,uuid:"D3C0AF62-D032-4252-8FCC-DEEA7247E964"}
 * @AllowToRunInFind
 */
function buscar_pendientes() 
{
	fSelecciono = 0
	globals.gtotal_saldo = 0
	// para traer solamente los que tienen saldo y son debito  
	if (globals.marca_primer_find == 0)
	{
		globals.marca_primer_find = 1
		var estado = forms.ctacte_pr_list_opago.controller.find()
		if (estado != true)
		{
			globals.DIALOGS.showWarningDialog('Aviso','Error al buscar movimientos pendientes','OK')
			return
		}
		forms.ctacte_pr_list_opago.saldo = '> 0'
		forms.ctacte_pr_list_opago.proveedor_id = forms.opago_1.proveedor_id
		
		if (forms.opago_1.fPagoTerceros == 1)
		{
			cod_mov = 15
		}
		else
		{
			cod_mov = "!15"
		}	
		var found = forms.ctacte_pr_list_opago.controller.search()
		
		if (found <= 0)
		{
			globals.DIALOGS.showWarningDialog('Aviso',"No hay movimientos de cuenta corriente con saldo",'OK')
		}
	}
}

/**
 * @properties={typeid:24,uuid:"59E2C77D-EE52-4042-85B6-CEF0A3DB54FF"}
 */
function confirmar_seleccion() 
{
	forms.opago_1.op_cabecera_to_op_detalle_comprob.deleteAllRecords()
	
	var found = databaseManager.getFoundSetCount(foundset)	
	if (found > 0)
	{
		fTotalComprob		 			= 0 
		globals.gtotal_neto_retencion 	= 0
		var aux_saldo = 0 
		for (var i = 1 ; i <= found ; i++)
		{		 
			var $rcd = foundset.getRecord(i)
			if ($rcd.marcado_ctacte == 1)
			{
				//grabar lo marcado en detalle_comprob
				forms.opago_1.op_cabecera_to_op_detalle_comprob.newRecord()
				forms.opago_1.op_cabecera_to_op_detalle_comprob.cod_mov 			= $rcd.cod_mov
				forms.opago_1.op_cabecera_to_op_detalle_comprob.fecha 				= $rcd.fecha_mov
				forms.opago_1.op_cabecera_to_op_detalle_comprob.importe				= globals.redondeo($rcd.saldo)
				forms.opago_1.op_cabecera_to_op_detalle_comprob.imp_original 		= globals.redondeo($rcd.saldo)
				forms.opago_1.op_cabecera_to_op_detalle_comprob.nro_comprobante 	= $rcd.nro_factura
				forms.opago_1.op_cabecera_to_op_detalle_comprob.letra 				= $rcd.letra
				forms.opago_1.op_cabecera_to_op_detalle_comprob.sucursal 			= $rcd.sucursal
				if ($rcd.concepto == null)
					$rcd.concepto = "";
				forms.opago_1.op_cabecera_to_op_detalle_comprob.concepto 			= $rcd.concepto
				forms.opago_1.op_cabecera_to_op_detalle_comprob.ctacte_pr_id 		= $rcd.ctacte_pr_id
				if ($rcd.ctacte_pr_to_tab_mov_pr.d_h == 1)
				{
				  	aux_saldo = globals.redondeo($rcd.saldo * (-1))
				}
				else
				{
				  	aux_saldo = globals.redondeo($rcd.saldo) 
				} 
				fTotalComprob += aux_saldo
				forms.opago_1.op_cabecera_to_op_detalle_comprob.neto_retencion 			= $rcd.neto_retencion  * $rcd.saldo / $rcd.importe
				forms.opago_1.op_cabecera_to_op_detalle_comprob.neto_retencion_signo 	= $rcd.neto_retencion_signo  * $rcd.saldo / $rcd.importe

				globals.gtotal_neto_retencion += forms.opago_1.op_cabecera_to_op_detalle_comprob.neto_retencion_signo
			}
		}
		forms.opago_1.f_neto_reten = globals.redondeo(globals.gtotal_neto_retencion)
	}
	fSelecciono = 1
	var $win = application.getWindow('ctacte_opago')
	$win.hide()
	$win.destroy()
}

/**
 *
 * @properties={typeid:24,uuid:"CE588806-C02F-4228-AAE3-9D56BD6E75B5"}
 */
function marcar_todo() 
{
	var $total = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $total; i++)
	{
		var $rcd = foundset.getRecord(i)
	  	$rcd.marcado_ctacte = 1
	}
	totales()	
}

/**
 *
 * @properties={typeid:24,uuid:"201A7094-CF3D-4AE1-83C6-B4DCADDD7998"}
 */
function desmarcar_todo() 
{
	fTotalComprob = 0
	var $total = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $total; i++)
	{
		var $rcd = foundset.getRecord(i)
	  	$rcd.marcado_ctacte = 0
	}
}

/**
 *
 * @properties={typeid:24,uuid:"9B4E9959-19B9-4186-8387-6E8A33A41228"}
 */
function totales() 
{
	fTotalComprob = 0 
	var aux_saldo = 0
 	var $total = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $total; i++)
	{
		var $rcd = foundset.getRecord(i)
	  	if ($rcd.marcado_ctacte == 1)
	  	{
		  	if ($rcd.ctacte_pr_to_tab_mov_pr.d_h == 1)
		 	{
			  	aux_saldo = globals.redondeo($rcd.saldo * (-1))
		  	}
		 	 else
		  	{
			  	aux_saldo = globals.redondeo($rcd.saldo) 
		  	}
		  	fTotalComprob += aux_saldo
	  	}
	}
	globals.opago_diferencia(forms.opago_1.fTipoOrden,forms.opago_1.fTotalPago);
}

/**
 * Handle hide window.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"E820D2A7-3984-4CFB-BF77-18595648B6B9"}
 */
function onHide(event) {
	if (fSelecciono)
	{
		globals.gtotal_comprob = fTotalComprob
	}
	return true
}
